/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Instrutor;

import Geral.ErroInternoException;
import java.io.Serializable;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Ivaldo
 */
@Stateless
public class RepositorioInstrutorJPA implements IRepositorioInstrutor, Serializable {

    @PersistenceContext
    private EntityManager em;

    public RepositorioInstrutorJPA() {
        this.em = Persistence.createEntityManagerFactory("HelpFitPU").createEntityManager();
    }

    @Override
    public void adicionar(Instrutor instrutor) throws ErroInternoException, InstrutorExistenteException {
        try {
            this.em.persist(instrutor);
        } catch (Exception e) {
            throw new ErroInternoException(e);
        }
    }

    @Override
    public void remover(long id_instrutor) throws ErroInternoException, InstrutorInexistenteException {
        Instrutor ins = this.buscarInstrutor(id_instrutor);
        try {
            this.em.remove(ins);
        } catch (Exception e) {
            throw new ErroInternoException(e);
        }
    }

    @Override
    public Instrutor buscarInstrutor(long id_instrutor) throws ErroInternoException, InstrutorInexistenteException {
        try {
            Instrutor ins = this.em.find(Instrutor.class, id_instrutor);
            if (ins == null) {
                throw new InstrutorInexistenteException();
            }
            return ins;
        } catch (InstrutorInexistenteException e) {
            throw e;
        } catch (Exception e) {
            throw new ErroInternoException(e);
        }
    }

    @Override
    public Instrutor buscarInstrutorPorNome(String nome) throws ErroInternoException, InstrutorInexistenteException {
        try {
            TypedQuery<Instrutor> query = this.em.createQuery("SELECT i FROM Instrutor i WHERE i.nome = :nome ", Instrutor.class);
            query.setParameter("nome", nome);
            return query.getSingleResult();
        }catch(NoResultException e){
            throw new InstrutorInexistenteException();
        }catch(Exception e){
            throw new ErroInternoException(e);
        }

    }

    @Override
    public void atualizar(Instrutor instrutor) throws ErroInternoException, InstrutorInexistenteException {
        this.buscarInstrutor(instrutor.getId());
        try {
            this.em.merge(instrutor);
        } catch (Exception e) {
            throw new ErroInternoException(e);
        }
    }

    @Override
    public Instrutor loginInstrutor(String login, String senha) throws ErroInternoException, InstrutorInexistenteException {
        try {
            TypedQuery<Instrutor> query = this.em.createQuery("SELECT i FROM Instrutor i WHERE i.login = :login AND i.senha = :senha", Instrutor.class);
            query.setParameter("login", login);
            query.setParameter("senha", senha);
            return query.getSingleResult();
        } catch (NoResultException e) {
            throw new InstrutorInexistenteException();
        } catch (Exception e) {
            throw new ErroInternoException(e);
        }
    }

    @Override
    public List<Instrutor> listaInstrutor(Instrutor instrutor) throws ErroInternoException {
        TypedQuery<Instrutor> listaInstrutor = this.em.createQuery("SELECT i from Instrutor i", Instrutor.class);
        return listaInstrutor.getResultList();
    }

}
